//OmusicList Class
function OmusicList()
{
};

OmusicList.prototype = 
    {
	getListPath : function()
	{
	    var file = DirIO.get("ProfD"); // %Profile% dir
	    file.append("omusiclist.xml");
	    return FileIO.path(file);
	},
	
	putList : function(doc, filepath)
	{
	    var serializer = new XMLSerializer();
	    var file;
	    if(filepath == undefined)
	    {
		file = DirIO.get("ProfD"); // %Profile% dir
		file.append("omusiclist.xml");
	    }
	    else
	    {
		file = FileIO.open(filepath);
	    }
	    var fileContents = serializer.serializeToString(doc);
	    if(fileContents.indexOf('<?xml version=')==-1)fileContents = '<?xml version="1.0" encoding="UTF-8"?>\r\n'+fileContents;
	    FileIO.write(file, fileContents, '', "UTF-8");
	},

	getList : function(filepath)
	{
	    var file;
	    if(filepath == undefined)
	    {
		file = DirIO.get("ProfD"); // %Profile% dir
		file.append("omusiclist.xml");
	    }
	    else
	    {
		file = FileIO.open(filepath);
	    }
	    
	    if(file.exists())
	    {
		var fileContents = FileIO.read(file, 'UTF-8');
		var domParser = new DOMParser();
		var dom = domParser.parseFromString(fileContents, "text/xml");
		return dom;
	    }
	    else
	    {
		return this.createList();
	    }
	},
	
	createList : function(filepath)
	{
	    var doc = document.implementation.createDocument("http://xspf.org/ns/0/", "playlist", null);
	    var playlist = doc.documentElement;
	    playlist.setAttribute("version", "1.0");

	    var tracklist = doc.createElement("trackList");
	    var nn = doc.createTextNode("\r\n\t");
	    playlist.appendChild(nn);
	    playlist.appendChild(tracklist);
	    var nn = doc.createTextNode("\r\n");
	    playlist.appendChild(nn);
	    this.putList(doc);
	    return doc;
	},
	
	mergeList : function(mdoc)
	{
	    var doc = this.getList();
	    var list = doc.documentElement;
	    
	    var tracklist = list.firstChild;
	    while(tracklist.nodeType != 1)tracklist = tracklist.nextSibling;
	    
	    var mtracks = mdoc.getElementsByTagName('track');
	    var b = tracklist.hasChildNodes();
	    var len = mtracks.length;
	    for(var i=0; i<len; i++)
	    {
		var mitem = mtracks[0];
		if(!b)
		{
     		    var nn = doc.createTextNode("\r\n\t\t");
		    tracklist.appendChild(nn);
		    tracklist.appendChild(mitem);
		}
		else
		{
     		    var lc = tracklist.lastChild;
		    var tt = tracklist.insertBefore(mitem, lc);
   	  	    var nn = doc.createTextNode("\r\n\t\t");
		    tracklist.insertBefore(nn, tt);
		}
	    }
	    if(!b)
	    {
		var nn = doc.createTextNode("\r\n\t");
		tracklist.appendChild(nn);			
	    }
	    this.putList(doc);
	},
	
	deleteFromList : function(num)
	{
	    var doc = this.getList();
	    var tracks = doc.getElementsByTagName('track');
	    var len = tracks.length;
	    if(num >= len) return;
	    var track = tracks[num];
	    track.parentNode.removeChild(track.previousSibling);
	    track.parentNode.removeChild(track);
	    this.putList(doc);
	},
	
	addToList : function(fileurl, filetitle)
	{
	    var doc = this.getList();
	    var list = doc.documentElement;

	    var tracklist = list.firstChild;
	    while(tracklist.nodeType != 1)tracklist = tracklist.nextSibling;

	    var track = doc.createElement('track');
	    var tit = doc.createElement('title');
	    var loc = doc.createElement('location');
	    var titcon = doc.createTextNode(filetitle);
	    var locurl = doc.createTextNode(fileurl);		

	    tit.appendChild(titcon);
	    loc.appendChild(locurl);
	    track.appendChild(tit);
	    track.appendChild(loc);

	    
	    if(tracklist.hasChildNodes())
	    {
		var ftrack = tracklist.firstChild;
		tracklist.insertBefore(track, ftrack);
		var nn = doc.createTextNode("\r\n\t\t");
		tracklist.insertBefore(nn, track);
	    }
	    else
	    {
		var nn = doc.createTextNode("\r\n\t\t");
		tracklist.appendChild(nn);
		tracklist.appendChild(track);
		var nn = doc.createTextNode("\r\n\t");
		tracklist.appendChild(nn);
	    }
	    this.putList(doc);
	},
	
	getOffset : function(num)
	{
	    var doc = this.getList();
	    var tracks = doc.getElementsByTagName('track');
	    var len = tracks.length;
	    if(Math.round(num) >= len) return 0;
	    var track = tracks[num];
	    var off = track.getAttribute('offset');
	    if(off)off = Math.round(off);
	    else off = 0;
	    return off;
	},
	
	setOffset : function(num, off)
	{
	    var doc = this.getList();
	    var tracks = doc.getElementsByTagName('track');
	    var len = tracks.length;
	    if(num >= len) return;
	    var track = tracks[num];
	    track.setAttribute('offset', off);
	    this.putList(doc);
	}	
    };